package com.chinaums.study.restful.service;

import com.chinaums.study.restful.entity.FaceCompareLog;
import com.chinaums.study.restful.entity.QFaceCompareLog;
import com.chinaums.study.restful.repository.FaceCompareLogRepository;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class FaceCompareLogService {

    @Autowired
    private FaceCompareLogRepository faceCompareLogRepository;

    public void save(final Map<?, ?> resp) {
        final FaceCompareLog entity = new FaceCompareLog();
        entity.setTranDate(LocalDate.parse((String) resp.get("TranDate")));
        entity.setTranTime(LocalTime.parse((String) resp.get("TranTime")));
        entity.setSeqNo((String) resp.get("ServSeqNo"));
        entity.setReturnCode((String) resp.get("ReturnCode"));
        entity.setReturnMsg((String) resp.get("ReturnMsg"));
        entity.setSmlrVal(Integer.valueOf((String) resp.get("SmlrVal")));
        entity.setChkRslt(Integer.valueOf((String) resp.get("ChkRsltFlg")));
        faceCompareLogRepository.save(entity);
    }

    public Iterable<FaceCompareLog> query(final String keyword) {
        final List<Predicate> conditions = new ArrayList<>();
        if (!StringUtils.isEmpty(keyword)) {
            conditions.add(QFaceCompareLog.faceCompareLog.seqNo.eq(keyword));
            conditions.add(QFaceCompareLog.faceCompareLog.returnMsg.like("%" + keyword + "%"));
        }
        return faceCompareLogRepository.findAll(ExpressionUtils.anyOf(conditions));
    }
}
